package sim

import (
	"context"

	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/sagoo-cloud/sagooiot/api/v1/sim"
	"github.com/sagoo-cloud/sagooiot/internal/model"
	"github.com/sagoo-cloud/sagooiot/internal/service"
	simfunc "github.com/sagoo-cloud/sagooiot/internal/task/sim"
)

var SimCollect = cSimSimCollect{}

type cSimSimCollect struct{}

// GetSimCollectList 获取列表
func (u *cSimSimCollect) GetSimCollectList(ctx context.Context, req *sim.GetSimCollectListReq) (res *sim.GetSimCollectListRes, err error) {
	var reqData = new(model.GetSimCollectListInput)
	gconv.Scan(req, &reqData)
	total, currentPage, dataList, err := service.SimSimCollect().GetSimCollectList(ctx, reqData)
	res = new(sim.GetSimCollectListRes)
	for i := 0; i < len(dataList); i++ {
		dataList[i].ActivationTime = dataList[i].ActivationTime[0:4] + "-" + dataList[i].ActivationTime[4:6] + "-" + dataList[i].ActivationTime[6:]
	}
	gconv.Scan(dataList, &res.Data)

	res.PaginationRes.Total = total
	res.PaginationRes.CurrentPage = currentPage
	return
}

// SimDetail 获取SIM卡详情
func (u *cSimSimCollect) SimDetail(ctx context.Context, req *sim.SimDetailReq) (res *sim.SimDetailRes, err error) {
	res = new(sim.SimDetailRes)
	data, err := service.SimSimCollect().SimDetail(ctx, req.Id)
	if err != nil {
		return
	}
	gconv.Scan(data, res)
	return
}

// SimDetail 获取SIM卡消耗流水 近一周、一月、一年折线图
func (u *cSimSimCollect) SimFlowByDate(ctx context.Context, req *sim.SimFlowByDateReq) (res *sim.SimFlowByDateRes, err error) {
	res = new(sim.SimFlowByDateRes)
	var in model.SimFlowByDate
	gconv.Scan(req, &in)
	data, err := service.SimSimCollect().SimFlowByDate(ctx, &in)
	if err != nil {
		return
	}
	gconv.Scan(res.Data, data.Data)
	return
}

// SyncSimCollectReq 同步物联网卡
func (u *cSimSimCollect) SyncSimCollect(ctx context.Context, req *sim.SyncSimCollectReq) (res *sim.SyncSimCollectRes, err error) {
	simfunc.SyncSimList(ctx, "ctwing")
	simfunc.SyncRealFlow(ctx, "ctwing")
	return
}

// GetSimCollectById 获取指定ID数据
//
//	func (u *cSimSimCollect) GetSimCollectById(ctx context.Context, req *sim.GetSimCollectByIdReq) (res *sim.GetSimCollectByIdRes, err error) {
//		data, err := service.SimSimCollect().GetSimCollectById(ctx, req.Id)
//		res = new(sim.GetSimCollectByIdRes)
//		gconv.Scan(data, &res)
//		return
//	}
//

// AddSimCollect 添加数据
func (u *cSimSimCollect) AddSimCollect(ctx context.Context, req *sim.AddSimCollectReq) (res *sim.AddSimCollectRes, err error) {
	userInfo := service.Context().GetLoginUser(ctx)
	if userInfo == nil {
		err = gerror.New("未登录或TOKEN失效,请重新登录")
		return
	}
	var data = model.SimCollectAddInput{}
	gconv.Scan(req, &data)
	// data.CreatedBy = userInfo.Id
	err = service.SimSimCollect().AddSimCollect(ctx, data)
	return
}

// EditSimCollect 修改数据
func (u *cSimSimCollect) EditSimCollect(ctx context.Context, req *sim.EditSimCollectReq) (res *sim.EditSimCollectRes, err error) {
	userInfo := service.Context().GetLoginUser(ctx)
	if userInfo == nil {
		err = gerror.New("未登录或TOKEN失效,请重新登录")
		return
	}
	var data = model.SimCollectEditInput{}
	gconv.Scan(req, &data)
	// data.UpdateBy = userInfo.Id //如果需要保存信息，把这个打开
	err = service.SimSimCollect().EditSimCollect(ctx, data)
	return
}

// DeleteSimCollect 删除数据
func (u *cSimSimCollect) DeleteSimCollect(ctx context.Context, req *sim.DeleteSimCollectReq) (res *sim.DeleteSimCollectRes, err error) {
	if len(req.Ids) == 0 {
		err = gerror.New("ID参数错误")
	}
	err = service.SimSimCollect().DeleteSimCollect(ctx, req.Ids)
	return
}
